package com.ykdl.member.kid.adapters;

import java.util.ArrayList;
import java.util.List;

import com.ykdl.member.kid.R;
import com.ykdl.member.KidApp;
import com.ykdl.member.kid.beans.BabyProfileBean;
import com.ykdl.member.kid.beans.BodyTypeBean;
import com.ykdl.member.kid.beans.ProfileBean;
import com.ykdl.member.kid.util.DateUtil;

import android.content.Context;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
/**
 * 身高，体重，头围数据
 * @author Pengfei
 *
 */
public class BodyAdapter extends BaseAdapter {
	
	private Context context;
	private List<BodyTypeBean> beans = new ArrayList<BodyTypeBean>();
	private BabyProfileBean baby;
	
	public BodyAdapter(Context context){
		this.context = context;
	}
	
	public void setBaby(BabyProfileBean baby) {
		this.baby = baby;
	}

	public List<BodyTypeBean> getBeans() {
		return beans;
	}

	public void addBeans(List<BodyTypeBean> beans) {
		if (beans != null && beans.size() != 0) {
			for (BodyTypeBean b : beans) {
				this.beans.add(b);
			}
		}
	}
	
	public void cleanData(){
		this.beans.clear();
	}

	@Override
	public int getCount() {
		if (beans == null) {
			return 0;
		} else {
			return beans.size();
		}
	}
	

	@Override
	public Object getItem(int position) {
		return beans.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			convertView = ((LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.item_body_type, null);
			holder = new ViewHolder();
			holder.txtAge = (TextView)convertView.findViewById(R.id.txtAge);
			holder.txtValue = (TextView)convertView.findViewById(R.id.txtValue);
			holder.txtDate = (TextView)convertView.findViewById(R.id.txtDate);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		BodyTypeBean body = (BodyTypeBean) getItem(position);
		// TODO 宝贝的年龄参数有待讨论
		long age = (long)(baby.getBirthday()*1000);
		String strAge = DateUtil.getBabyAge(age, (long)(body.getRecord_time() * 1000));
		holder.txtAge.setText(strAge);
		holder.txtValue.setText(Float.toString(body.getValue()));
		
		if (body.getType() == BodyTypeBean.TYPE_HEIGHT) {
			holder.txtValue.setText(context.getString(R.string.fittool_height)+body.getValue()+context.getString(R.string.unit_cm));
		} else if (body.getType() == BodyTypeBean.TYPE_WEIGHT) {
			holder.txtValue.setText(context.getString(R.string.fittool_weight)+body.getValue()+context.getString(R.string.unit_kg));
		} else if (body.getType() == BodyTypeBean.TYPE_HEAD) {
			holder.txtValue.setText(context.getString(R.string.fittool_head)+body.getValue()+context.getString(R.string.unit_cm));
		}
		holder.txtDate.setText(DateUtil.getDateData(body.getRecord_time()));
		return convertView;
	}
	
	class ViewHolder {
		TextView txtAge;
		TextView txtValue;
		TextView txtDate;
	}
}
